package judesart.app.client.ui.admin.profile;

import java.util.List;

import judesart.app.client.controller.ExhibitionController;
import judesart.app.client.controller.ExhibitionControllerAsync;
import judesart.app.shared.ArtworkShort;

import com.allen_sauer.gwt.log.client.Log;
import com.extjs.gxt.ui.client.data.BaseListLoader;
import com.extjs.gxt.ui.client.data.BaseModel;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelReader;
import com.extjs.gxt.ui.client.data.ListLoadResult;
import com.extjs.gxt.ui.client.data.ListLoader;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.Composite;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.ListView;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class ExhibitionArtworksPanel extends Composite {
    ExhibitionControllerAsync service = ExhibitionController.Util.getInstance();

    private final LayoutContainer layoutContainer;
    private final ExhibitionDetailPanel parent;
    private final ListLoader<ListLoadResult<ArtworkShort>> loader;

    public ExhibitionArtworksPanel(ExhibitionDetailPanel parent) {
        this.parent = parent;
        layoutContainer = new LayoutContainer();
        layoutContainer.setWidth(798);
        layoutContainer.setAutoHeight(true);
        layoutContainer.setBorders(false);

        RpcProxy<List<ArtworkShort>> proxy = new RpcProxy<List<ArtworkShort>>() {
            @Override
            protected void load(Object loadConfig,
                    AsyncCallback<List<ArtworkShort>> callback) {
                searchArtworks(callback);
            }
        };

        loader = new BaseListLoader<ListLoadResult<ArtworkShort>>(proxy, new BeanModelReader());
        ListStore<BeanModel> store = new ListStore<BeanModel>(loader);

        final ListView<BeanModel> artworkListView = new ListView<BeanModel>(store);
        artworkListView.setLoadingText("loading...");
        artworkListView.setBorders(false);
        artworkListView.setSize("796", "420");
        artworkListView.setTemplate(getTemplate());
        artworkListView.setItemSelector("div.thumb-wrap");
        artworkListView.getSelectionModel().addListener(Events.SelectionChange,
                new Listener<SelectionChangedEvent<BaseModel>>() {
                    public void handleEvent(SelectionChangedEvent<BaseModel> be) {
                        onSelectionChanged(be);
                    }
                });

        layoutContainer.add(artworkListView);

        initComponent(layoutContainer);
    }

    protected ExhibitionDetailPanel getParentPanel() {
        return parent;
    }

    public void refresh() {
        if (getParentPanel().getExhibition() != null
                && getParentPanel().getExhibition().getId() != null) {
            loader.load();
            layoutContainer.layout();
        }
    }

    private native String getTemplate() /*-{
        return ['<tpl for=".">', 
        '<div style="border: 1px solid #FFFFFF;float:left;margin:4px 0 4px  4px; padding:2px;width:244px; height: 300px;" class="thumb-wrap" id="{id}">', 
        '<div style="width: 240px;" class="thumb"><img src="/image?id={imageId}&size=240x240"></div>', 
        '<div style="width: 100%; text-align: center; color: #999999;">{title}</div>',
        '</div>',
        '</tpl>', 
        '<div class="x-clear"></div>'].join("");
    }-*/;

    private void onSelectionChanged(SelectionChangedEvent<BaseModel> be) {
        Object source = be.getSource();
        Log.debug("source [" + source + "]");
        if (be.getSelection().size() == 0) {
            return;
        }
        final BaseModel model = be.getSelection().get(0);
    }

    private void searchArtworks(AsyncCallback<List<ArtworkShort>> callback) {
        Log.debug(" >> load artworks ");
        service.findExhibitionArtworks(
                getParentPanel().getExhibition().getId(), callback);
    }

}
